home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / ircii2-6.zip / SRC\IRCII-2.6\SOURCE\LOG.C < prev    next >
C/C++ Source or Header  |  1994-12-29  |  3KB  |  162 lines

  1. /*
  2.  * log.c: handles the irc session logging functions 
  3.  *
  4.  *
  5.  * Written By Michael Sandrof
  6.  *
  7.  * Copyright(c) 1990 
  8.  *
  9.  * See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT 
  10.  */
  11.  
  12. #ifndef lint
  13. static    char    rcsid[] = "@(#)$Id: log.c,v 1.12 1994/10/09 06:39:35 mrg Stab $";
  14. #endif
  15.  
  16. #include "irc.h"
  17.  
  18. #include <sys/stat.h>
  19.  
  20. #include "log.h"
  21. #include "vars.h"
  22. #include "output.h"
  23. #include "ircaux.h"
  24.  
  25. #if defined(POSIX)
  26. # define fchmod(path, mode) chmod(path, mode)
  27. #endif
  28.  
  29. FILE    *irclog_fp;
  30.  
  31. FILE    *
  32. do_log(flag, logfile, fp)
  33.     int    flag;
  34.     char    *logfile;
  35.     FILE    *fp;
  36. {
  37.     time_t    t;
  38.  
  39.     if (logfile == (char *) 0)
  40.         return ((FILE *) 0);
  41.     t = time(0);
  42.     if (flag)
  43.     {
  44.         if (fp)
  45.             say("Logging is already on");
  46.         else
  47.         {
  48. #ifdef DAEMON_UID
  49.             if (getuid() == DAEMON_UID)
  50.             {
  51.                 say("You are not permitted to use LOG");
  52.                 /* fp = (FILE *) 0;  unused */
  53.             }
  54.             else
  55.             {
  56. #endif /* DAEMON_UID */
  57.                 say("Starting logfile %s", logfile);
  58.                 if ((fp = fopen(logfile, "a")) != NULL)
  59.                 {
  60. #ifndef _Windows
  61. #ifdef NEED_FCHMOD
  62.                     chmod(logfile, S_IREAD | S_IWRITE);
  63. #else
  64. #ifndef _IBMR2
  65.                     fchmod(fileno(fp),S_IREAD | S_IWRITE);
  66. #else
  67.                     int fd = (int) fileno(fp);
  68.                     fchmod((char *) &fd, S_IREAD |
  69.                             S_IWRITE);
  70. #endif /* !_IBMR2 */
  71. #endif /* M_UNIX */
  72. #endif _Windows
  73.                     fprintf(fp, "IRC log started %.16s\n",
  74.                             ctime(&t));
  75.                     fflush(fp);
  76.                 }
  77.                 else
  78.                 {
  79.                     say("Couldn't open logfile %s: %s",
  80.                         logfile, strerror(errno));
  81.                     fp = (FILE *) 0;
  82.                 }
  83. #ifdef DAEMON_UID
  84.             }
  85. #endif /* DAEMON_UID */
  86.         }
  87.     }
  88.     else
  89.     {
  90.         if (fp)
  91.         {
  92.             fprintf(fp, "IRC log ended %.16s\n", ctime(&t));
  93.             fflush(fp);
  94.             fclose(fp);
  95.             fp = (FILE *) 0;
  96.             say("Logfile ended");
  97.         }
  98.     }
  99.     return (fp);
  100. }
  101.  
  102. /* logger: if flag is 0, logging is turned off, else it's turned on */
  103. void
  104. logger(flag)
  105.     int    flag;
  106. {
  107.     char    *logfile;
  108.  
  109.     if ((logfile = get_string_var(LOGFILE_VAR)) == (char *) 0)
  110.     {
  111.         say("You must set the LOGFILE variable first!");
  112.         set_int_var(LOG_VAR, 0);
  113.         return;
  114.     }
  115.     irclog_fp = do_log(flag, logfile, irclog_fp);
  116.     if ((irclog_fp == (FILE *) 0) && flag)
  117.         set_int_var(LOG_VAR, 0);
  118. }
  119.  
  120. /*
  121.  * set_log_file: sets the log file name.  If logging is on already, this
  122.  * closes the last log file and reopens it with the new name.  This is called
  123.  * automatically when you SET LOGFILE. 
  124.  */
  125. void
  126. set_log_file(filename)
  127.     char    *filename;
  128. {
  129.     char    *expand;
  130.  
  131.     if (filename)
  132.     {
  133.         if (strcmp(filename, get_string_var(LOGFILE_VAR)))
  134.             expand = expand_twiddle(filename);
  135.         else
  136.             expand = expand_twiddle(get_string_var(LOGFILE_VAR));
  137.         set_string_var(LOGFILE_VAR, expand);
  138.         new_free(&expand);
  139.         if (irclog_fp)
  140.         {
  141.             logger(0);
  142.             logger(1);
  143.         }
  144.     }
  145. }
  146.  
  147. /*
  148.  * add_to_log: add the given line to the log file.  If no log file is open
  149.  * this function does nothing. 
  150.  */
  151. void
  152. add_to_log(fp, line)
  153.     FILE    *fp;
  154.     char    *line;
  155. {
  156.     if (fp)
  157.     {
  158.         fprintf(fp, "%s\n", line);
  159.         fflush(fp);
  160.     }
  161. }
  162.